// pages/zhichu/zhuchu.js

Page({
  data: {
    poptext: "",
    addcash:0,
    totalspan: 0,
    visible2: false,
    showData: [],
    testData: [{
        type: 3,
        money: 500,
        time: "202202141043",
        note: "牛的呀"
      },
      {
        type: 4,
        money: 500,
        time: "202202141105",
        note: "666AbC"
      },
      {
        type: 2,
        money: 500,
        time: "202202151043",
        note: "住了好久"
      },
      {
        type: 2,
        money: 500,
        time: "202202151043",
        note: "贼贵"
      },
    ],
    //祝
    buttons: [{
        label: '其他',
        icon: '../../images/down.png',
      },
      {
        label: '住宿',
        icon: '../../images/down.png',
      },
      {
        label: '娱乐',
        icon: '../../images/down.png',
      },
      {
        label: '购物',
        icon: '../../../images/down.png',
      },

      {
        label: '交通',
        icon: '../../images/down.png',
      },
      {
        label: '餐饮',
        icon: '../../../images/down.png',
      },
    ],
    scrollTop: 0,
    displayValue2: '请选择',
    value2: '',
    options2: [{
        title: '餐饮', //0
        value: 'canyin',
      },
      {
        title: '交通', //1
        value: 'jiaotong',
      },
      {
        title: '购物', //2
        value: 'gouwu',
      },
      {
        title: '娱乐', //3
        value: 'yule',
      },
      {
        title: '住宿', //4
        value: 'zhusu',
      },
      {
        title: '其他', //5
        value: 'qita',
      }
    ],

  },
  onBlur(e){
    this.setData({
      addcash:e.detail.value
    })
  },
  setValue(values, key) {
    this.setData({
      [`value${key}`]: values.value,
      [`displayValue${key}`]: values.label,
    })
  },
  onConfirm(e) {
    const {
      index
    } = e.currentTarget.dataset
    this.setValue(e.detail, index)
    console.log(`onConfirm${index}`, e.detail)
    if (e.detail.selectedIndex.length > 0) {
      this.typechange(e.detail.selectedIndex)
    } else {
      this.dealPay()
      this.stastic()
    }
  },
  onValueChange(e) {
    const {
      index
    } = e.currentTarget.dataset
    console.log(`onValueChange${index}`, e.detail)
  },
  onClick(e) {
    console.log(e)
    this.open2(5 - e.detail.index);
  },
  addData(e) {
    if (this.data.addcash==0){
      console.log("不能为0")
    }
    else{
      this.setData({
        visible2:false
      })
    }
    
  },
  open2(e) {
    console.log("pop  " + e)
    this.setData({
      visible2: true,
      poptext: this.data.options2[e].title
    })
  },
  onClose2() {
    this.setData({
      visible2: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var timestamp = Date.parse(new Date());
    console.log("当前时间戳为：" + timestamp);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.dealPay();
    this.stastic();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  dealPay: function () {
    this.setData({
      showData: []
    })
    for (var o in this.data.testData) {
      // console.log(this.data.testData)
      let it = this.data.testData[o]
      // console.log("it",it)
      let dectime = it.time.substr(4, 4)
      let month = it.time.substr(4, 2);
      let day = it.time.substr(6, 2);
      let sec = it.time.substr(8, 2) + ':' + it.time.substr(10, 2);
      let newit = {};
      newit.name = this.data.options2[it.type].title
      newit.month = month
      newit.day = day
      newit.cash = it.money
      newit.note = it.note
      newit.type = it.type
      newit.sec = sec
      let i = this.findtime(this.data.showData, dectime)

      if (i < 0) {

        let newdata = {}
        let newmes = [];
        newmes.push(newit);
        newdata.time = dectime
        newdata.mes = newmes
        this.data.showData.push(newdata)

      } else {
        this.data.showData[i].mes.push(newit)
      }

    }
    this.setData({
      showData: this.data.showData
    })
    console.log(this.data.showData)
  },
  findtime: function (a, b) {
    let res = -1;
    for (var i in a) {
      if (a[i].time == b) {
        res = i;
      }
    }
    return res;
  },
  stastic() {
    this.setData({
      totalspan: 0
    })
    for (let i in this.data.showData) {
      let it = this.data.showData[i];
      let sumlit = 0
      for (let j in it.mes) {
        sumlit += it.mes[j].cash
      }
      it.sum = sumlit
      this.data.totalspan += sumlit
    }

    this.setData({
      showData: this.data.showData,
      totalspan: this.data.totalspan
    })
  },

  typechange(a) {
    this.setData({
      showData: []
    })
    for (let i in this.data.testData) {
      let it = this.data.testData[i]
      if (a.indexOf(it.type) >= 0) { //如果在选择的索引里面，就开始组装item
        let dectime = it.time.substr(4, 4)
        let month = it.time.substr(4, 2);
        let day = it.time.substr(6, 2);
        let sec = it.time.substr(8, 2) + ':' + it.time.substr(10, 2);
        let newit = {};
        newit.name = this.data.options2[it.type].title
        newit.month = month
        newit.day = day
        newit.cash = it.money
        newit.note = it.note
        newit.type = it.type
        newit.sec = sec
        let i = this.findtime(this.data.showData, dectime)
        if (i < 0) {

          let newdata = {}
          let newmes = [];
          newmes.push(newit);
          newdata.time = dectime
          newdata.mes = newmes
          this.data.showData.push(newdata)

        } else {
          this.data.showData[i].mes.push(newit)
        }

      }
      this.stastic();
      this.setData({
        showData: this.data.showData
      })
      console.log(this.data.showData)
    }
  },

})